package com.augrain.microframework.trace.common.spanid;

import com.augrain.microframework.trace.context.TraceContext;

/**
 * spanId生成器
 *
 * @author biaoy
 * @since 2021/08/21
 */
public class SpanIdGenerator {
    public static final String INITIAL_VALUE = "1";

    private SpanIdGenerator() {

    }

    public static String generateNextSpanId() {
        synchronized (TraceContext.getTraceId()) {
            String currentSpanId = TraceContext.getSpanId();

            int currentSpanIdIndex = TraceContext.getSpanIdIndex();
            TraceContext.updateSpanIdIndex(currentSpanIdIndex + 1);
            return String.format("%s.%s", currentSpanId, currentSpanIdIndex);
        }
    }

}
